<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h3 class="page-header">
                <span>投票活动分享设置</span>
            </h3>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-lg-12">
            <div class="panel-group" id="accordion">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" data-parent="#accordion"
                               href="#collapseOne">基本设置
                            </a>
                        </h4>
                    </div>
                    <div id="collapseOne" class="panel-collapse collapse in">
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-6">
                                    <form role="form" method="post" action="/User/praiseeat/share"
                                          enctype="multipart/form-data">
                                        <input type="hidden" id='id' name="id" value="{$praise['id']}">

                                        <div class="form-group">
                                            <label>名称</label>
                                            <input type='text' class="form-control" name="title" value="{$praise['title']}"
                                                   disabled>
                                        </div>
                                        <div class="form-group">
                                            <label>分享标题</label>
                                            <!-- <input class="form-control" name="info" value="{$praise['info']}" placeholder="请输入活动简介"> -->
                                            <input type="text" name="share_title" id='share_title' class='form-control' value="<?php
                                                if(isset($praise['info']) && !empty($praise['info'])){
                                        		$data_info = json_decode($praise['info'],true);
                                        		echo $data_info['share_title'];
                                        	}
                                            ?>">
                                        </div>
                                        <div class="form-group">
                                            <label>分享描述</label>
										<textarea class="form-control" id='share_desc' name="share_desc"><?php
                                        	if(isset($praise['info']) && !empty($praise['info'])){
                                        		$data_info = json_decode($praise['info'],true);
                                        		echo $data_info['share_desc'];
                                        	}
                                        ?></textarea>
                                        </div>
                                        <div class="form-group">
                                            <label  class="col-sm-2 col-sm-2 control-label">分享图片</label>
                                            <div class="col-sm-10">
                                                <?php
                                                $img_tag = '<img width="100%" height="200px" src="" alt=" " id="classimg">';
                                                if(!empty($praise) && !empty($praise['info'])){
                                                    $data_info = json_decode($praise['info'],true);
                                                    $img_tag = '<img width="100%" height="200px" id="classimg" src="' . $data_info['share_pic'] . '" alt=" " >';
                                                }
                                                $img_tag .= '<input type="file" id="share_pic" >';
                                                echo $img_tag;
                                                ?>
                                            </div>
                                        </div>
                                        <button type="submit" id='submit' class="btn btn-success">保存</button>
                                        <button type="button" class="btn btn-default" onclick="history.back(-1);">取消</button>
                                    </form>
                                </div>
                                <!-- /.col-lg-6 (nested) -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var picdata = '';
    $(function(){
        var img_upload = document.getElementById("share_pic");
        img_upload.addEventListener('change',readFile,false);
    });
    function readFile(){
        var file = this.files[0];
        if(!/image\/\w+/.test(file.type)){
            alert("请确保文件为图像类型");
            return false;
        }
        var reader=new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function() {
            $("#classimg").attr("src", this.result);
            picdata = this.result;
        }
    }
    $(".need").each(function(){
        $(this).prepend("<span style='color:red'>*</span>");
    });
    $("#submit").click(function(){
        var data = {
            id : $("#id").val(),
            share_title : $("#share_title").val(),
            share_desc : $("#share_desc").val(),
            share_pic : picdata
        };
        var url = $("form").attr("action");
        $.ajax({
            type:'post',
            dataType:'json',
            data:data,
            url:url,
            success:function(res){
                if(res.code == 1){
                    alert("提交成功");
                    window.location = '/User/praiseeat/index';
                }else{
                    alert(res.msg);
                }
            }
        });
        return false;
    });
</script>
